from app.core.img_opencv import *
from app.core.img_ocr import *
# region 万灵塔
def wlt_work(spin_box_value, selected_index, scale):
    """
    万灵塔挑战过程
    """
    image_paths = ['app_img/public_img/continue_game.png',
                'app_img/public_img/yes.png']
    
    for _ in range(spin_box_value):
        click_image('app_img/pet_challenge/choose.png',threshold=0.7, image_num=selected_index, scale=scale)

        click_image('app_img/pet_challenge/3.png', threshold=0.7, scale=scale)

        if (is_image_now('app_img/pet_challenge/5.png', threshold=0.7, scale=scale)):
            break

        wait_many_image(image_paths, threshold=0.8, scale=scale)
        if_exist_click_image('app_img/public_img/continue_game.png', scale=scale)
        click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale)
        wait_image('app_img/pet_challenge/2.png', threshold=0.7, scale=scale)
# region 葬灵洞
def zld_work(scale):
    """
    葬灵洞挑战过程
    """
    wait_click_image('app_img/zld_challenge/zld_click_no.png', scale=scale)
    
    while True:
        if (not is_image_now('app_img/zld_challenge/zld_tz.png', threshold=0.8, scale=scale)):
            # 葬灵洞探索逻辑
            if(if_exist_click_image('app_img/zld_challenge/start.png', scale=scale)):
                if (is_image_now('app_img/zld_challenge/zld_start_no.png', scale=scale)):
                    # 避免字幕的影响
                    click_image('app_img/public_img/no.png', scale=scale)
                else:
                    if_exist_click_image('app_img/public_img/continue_game.png', scale=scale) # 宝箱弹窗
                    if_exist_click_image('app_img/public_img/get_reward.png', scale=scale)    # 宠物灵石弹窗
                    break
            else:
                break
        else:
            # 葬灵洞挑战逻辑
            click_image('app_img/zld_challenge/zld_tz.png',threshold=0.8, scale=scale)
            if_exist_click_image('app_img/public_img/yes.png', scale=scale)
            wait_click_image('app_img/public_img/yes.png', scale=scale)
            wait_image('app_img/pet_challenge/2.png', threshold=0.7, scale=scale)

# region 斗宠挑战
def douchong_work(selected_index,scale):
    """
    斗宠挑战过程
    """
    while True:
        wait_click_image('app_img/douchong_challenge/1.png', threshold=0.7, scale=scale)
        time.sleep(1)
        if is_image_now('app_img/douchong_challenge/1_1.png', threshold=0.8, scale=scale):
            break
    if_exist_click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale)
    wait_click_image('app_img/douchong_challenge/2.png', threshold=0.7, scale=scale)
    wait_image('app_img/douchong_challenge/3.png', threshold=0.7, scale=scale)
    
    position = find_grid_patterns('app_img/douchong_challenge/3.png', scale=scale, threshold=0.8)

    h_pos=find_grid_patterns('app_img/douchong_challenge/4_1.png', scale=scale, threshold=0.8)
    e_pos=find_grid_patterns('app_img/douchong_challenge/4_2.png', scale=scale, threshold=0.8)
    h_pos_2=find_grid_patterns('app_img/douchong_challenge/5.png', scale=scale, threshold=0.8)

    # 获取模板图片尺寸（需要实际图片尺寸）
    template_h = cv2.imread(str(root_dir/'app_img/douchong_challenge/4_1.png'), 0).shape
    template_e = cv2.imread(str(root_dir/'app_img/douchong_challenge/4_2.png'), 0).shape
    template_h_2 = cv2.imread(str(root_dir/'app_img/douchong_challenge/5.png'), 0).shape

    # 我的战力坐标
    h_top_right = (
        int(h_pos[0][0] + (template_h[1] * scale)/2),  # 右上角x
        int(h_pos[0][1] - (template_h[0] * scale)/2)   # 右上角y
    )
    e_bottom_left = (
        int(e_pos[0][0] - (template_e[1] * scale)/2),  # 左下角x
        int(e_pos[0][1] + (template_e[0] * scale)/2)   # 左下角y
    )
    my_power_pos=(
        h_top_right[0], h_top_right[1],
        e_bottom_left[0] - h_top_right[0],
        e_bottom_left[1] - h_top_right[1]
    )

    # 敌人的战力坐标
    enemy_region = (
        int(h_pos_2[0][0] - (template_h_2[1] * scale)/2),  # 左边界：中心x - 半宽
        int(h_pos_2[0][1] - (template_h_2[0] * scale)/2),   # 上边界：中心y - 半高
        int(h_pos_2[0][0] + (template_h_2[1] * scale)/2),   # 右边界：中心x + 半宽
        int(h_pos_2[0][1] + (template_h_2[0] * scale)/2)    # 下边界：中心y + 半高
    )
    screen_w, _ = pyautogui.size()  # 获取屏幕尺寸
    my_power_pos_2 = (
        enemy_region[0], 
        enemy_region[1],
        screen_w - enemy_region[0],  # 从当前x到屏幕最右侧
        enemy_region[3] - enemy_region[1]
    )

    temp=img_ocr(my_power_pos)  
    my_power=temp[0]
    
    while True:
        if selected_index==1:
            mouse_click(position[3][0],position[3][1])
        elif selected_index==2 or selected_index==3 or selected_index==4:
            enemy_power=img_ocr(my_power_pos_2)
            percentage=1-0.10*(selected_index-2)
            for i in [3,2,1,0,-1]:
                if i==-1:
                    mouse_click(position[0][0],position[0][1])
                    break
                if my_power*percentage>int(enemy_power[i]):
                    mouse_click(position[i][0],position[i][1])
                    break
        elif selected_index==5:
            mouse_click(position[0][0],position[0][1])

        if is_image_now('app_img/douchong_challenge/6.png',threshold=0.7, scale=scale):
            break
        if is_image_now('app_img/douchong_challenge/3_1.png',threshold=0.7, scale=scale):
            click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale)
        wait_many_image(['app_img/public_img/yes.png','app_img/public_img/continue_game.png','app_img/douchong_challenge/3.png'],threshold=0.7, scale=scale)
        time.sleep(2)
        if_exist_click_image('app_img/public_img/continue_game.png',threshold=0.7, scale=scale)
        if if_exist_click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale):
            pass
        else:
            continue
        wait_many_image(['app_img/public_img/yes.png', 'app_img/douchong_challenge/3.png'],threshold=0.7, scale=scale)
        time.sleep(2)
        if_exist_click_image('app_img/public_img/yes.png', scale=scale)